﻿Ext.define("PSI.BaseCode.Admin.MainForm", {

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            // title: "码表结构管理",
            border: false,
            bodyBorder: false,
            layout: "border",
            items: [
                this.basecodeClassPanle(),
                this.basecodeAttrPanle()
            ]
        });

        me.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    basecodeClassPanle: function () {
        var me = this;
        var pan = Ext.create("Ext.panel.Panel", {
            region: "west",
            height: "100%",
            width: 500,
            title: "码表分类",
            tbar: [
                { xtype: 'button', text: '新增码表分类', iconCls: "psi-button-add", handler: this.showBaseCodeCategoryWin, scope: this },
                { xtype: 'button', text: '编辑码表分类', iconCls: "psi-button-edit", scope: this },
                { xtype: 'button', text: '删除码表分类', iconCls: "psi-button-delete", scope: this }
            ],
            collapsible: true,
            split: true,
            layout: "fit",
            border: 1,
            items: [
               me.baseCodeTree()
            ]
        })

        return pan;
    },

    basecodeAttrPanle: function () {
        var me = this;
        var pans = Ext.create("Ext.panel.Panel", {

            region: "center",
            height: "100%",
            width: 500,
            border: false,
            layout: "border",
            items: [
              this.basecodeAttrTopPanle(),
               me.basecodeAttrDownPanle()
            ]
        })

        return pans;
    },

    basecodeAttrTopPanle: function () {

        var pan = Ext.create("Ext.panel.Panel", {
            region: "north",
            tbar: [
               { xtype: 'button', text: '新增码表属性', iconCls: "psi-button-add", handler: this.showMetadataWin, scope: this },
               { xtype: 'button', text: '编辑码表属性', iconCls: "psi-button-edit", scope: this },
               { xtype: 'button', text: '删除码表属性', iconCls: "psi-button-delete", scope: this }
            ],
            height: "50%",
            width: "100%",
            title: "码表主属性",
            border: 1,
            items: [
                this.metadataPropertyGrid()
            ]
        })

        return pan;
    },

    basecodeAttrDownPanle: function () {
        var me=this;
        var pan = Ext.create("Ext.panel.Panel", {
            region: "south",
            height: "49%",
            width: "100%",
            title: "码表列属性",
            layout:"border",
            border: 1,
            items: [
                me.bownPanelLeft(),
                me.downPanelRight()
               
            ]
        })

        this.downpanel = pan;
        return pan;

    },

    bownPanelLeft:function(){
        var pan = Ext.create("Ext.panel.Panel", {
            region: "west",
            height: "100%",
            width: "70%",
          //  title: "码表列属性",
            layout: "fit",
            //split: true,
            //collapsible: true,
            border: false,
            items: [

              this.columsMetadtaGrid()
            ]
        })
        return pan;
    },

    downPanelRight:function(){
        var pan = Ext.create("Ext.panel.Panel", {
            region: "center",
            height: "100%",
            width: "30%",
          //  title: "码表列属性",
            layout: "fit",
            dockedItems: [{
                xtype: 'toolbar',
                dock: 'top',
                border:"0 0 0 1",
                //bodyStyle:'border-width:0 1px 0 0',
                items: [ 
                    { xtype: 'button', text: '编辑保存',iconCls: "psi-button-edit", handler: this.addMetadataofcategory, scope: this }
                ]
            }],
            
            bodyStyle: {
                background: "red",
              //  padding: '10px',
                borderWidth:'1px 0 0 1px'
            },
            items: [
               this.columsPreporGrid()
            ]
        })
        return pan;
    },

    showBaseCodeCategoryWin: function () {

        var form = Ext.create("PSI.BaseCode.Admin.Category.CategoryEditForm");

        form.show();
    },

    baseCodeTree: function () {

        var me = this;


        Ext.define("PSIOrgModel", {
            extend: "Ext.data.Model",
            fields: ["id", "text", "expandable", "expanded", "leaf", "children", "tableName"]
        });


        var orgStore = Ext.create("Ext.data.TreeStore", {
            id: "treestore",
            model: "PSIOrgModel",
            proxy: {
                type: "ajax",
                actionMethods: { read: 'POST' },
                url: "/BaseCodeAdminCategory/GetCateGoryTreeItem"
            }
        });

        var tree = Ext.create('Ext.tree.Panel', {
            // title: '简单树形',
            id:"caregorytree",
            border: false,
            rootVisible: false,                 // 这个比较重要  隐藏根节点
            useArrows: true,                    // 树结构显示风格
            hideHeaders: true,                  // 隐藏标题
            renderTo: Ext.getBody(),
            store: "treestore",
            columns: {
                items: [{
                    xtype: 'treecolumn',
                    dataIndex: 'text',
                    text: "树根",
                    flex: 1
                }]
            },
            listeners: {

                select: function (mes, record, index, eOpts) {
                    var isleaf = record.raw.leaf;
                    if (isleaf != null) {
                        Ext.create("Ext.data.Store", {
                            proxy: {
                                type: "ajax",
                                url: "/BaseCodeAdminMetadata/GetSing",
                                extraParams: { id: record.raw.id },
                                actionMethods: { read: 'POST' },
                                reader: {
                                    type: 'json',
                                    root: 'Data'
                                }
                            },
                            fields: ['basecodeName', 'tableName', 'baseCodeMemo'],
                            listeners: {
                                load: function (mes, records, successful, eOpts) {
                                    if (successful) {
                                        //var cc = records[0].data;
                                        //cc.delete("");

                                        var pg = me.MetadataPG.setSource(records[0].data);
                                        var downgdst = me.downgrid.getStore();

                                        downgdst.setProxy({
                                                type: "ajax",
                                                url: "/BaseCodeAdminMetadataColumn/GetItem",
                                                actionMethods: { read: "POST" },
                                                extraParams: { clid: record.raw.id },
                                                reader: {
                                                    type: "json",
                                                    root: "Data"
                                                }
                                        });

                                        downgdst.load();

                                    }
                                }
                            },
                            autoLoad: true
                        });
                    }
                }
            }
        })

        this.orgTree = tree;

        return tree;

    },

    showMetadataWin: function () {

        var tree = this.orgTree;
        var item = tree.getSelectionModel().getSelection();

        if (item == null || item.length != 1) {

            PSI.MsgBox.showInfo("请选择创建码表所在分类");
            return;
        }
        var islef = item[0].raw.leaf;

        if (islef ==true ) {
            PSI.MsgBox.showInfo("请选择码表分类");
            return;
        }
        var parementid = item[0].raw.id;


        var form = Ext.create("PSI.BaseCode.Admin.Metadata.MetadataEditForm");

        form.show(parementid);
       
    },
 
    metadataPropertyGrid: function () {
        var pg = Ext.create("Ext.grid.property.Grid", {
            renderTo: Ext.getBody(),
            width: 300,
            height: 300,
            border: false,
            propertyNames: { basecodeName: "码表中文名", tableName: "码表数据库名称", baseCodeMemo: "码表用途" },
            source: { }
        });

        this.MetadataPG = pg;
        return pg;
    },

    columsMetadtaGrid: function () {
        var me = this;
        //var form = Ext.create("PSI.BaseCode.Admin.Metadata.Column");
        //form.show(mdid);
        Ext.define("MCMolde", {
            extend: "Ext.data.Model",
            fields: ["columnId", "basecodeId", "columnTitle", "columnName", "columnType", "columnLength", "defultValue", "showOrder", "isVisible", "displayFmt"]
        });

        var storeGrid = Ext.create("Ext.data.Store", {
            model: "MCMolde",
            autoLoad: false,
            data: []
        });

        var grid = Ext.create("Ext.grid.Panel", {
            //  title: "人员列表",
            tbar: [
                { xtype: 'button', text: '新增码表字段', iconCls: "psi-button-add", handler: this.showAddcolumnWin, scope: this },
            ],
            border:false,
            columns: [
                { header: "id", dataIndex: "columnId",hidden:true },
                { header: "码表id", dataIndex: "basecodeId", hidden: true },
                { header: "列的中文名", dataIndex: "columnTitle" },
                { header: "列在数据库表的字段名", dataIndex: "columnName",width:150 },
                { header: "列的数据库中的类型", dataIndex: "columnType",width:140 },
                { header: "列的长度", dataIndex: "columnLength" },
                { header: "列的默认值", dataIndex: "defultValue" },
                { header: "列的显示次序", dataIndex: "showOrder" },
                { header: "列对用户是否可见", dataIndex: "isVisible",width:140 },
                { header: "显示格式", dataIndex: "displayFmt" }
            ],
            store: storeGrid,
            listeners: {
                select: function (mes, record, index, eOpts) {
                    var cc = record.data;
                    delete cc.columnid;
                    delete cc.basecodeid;
                    me.MetadataColumnPG.setSource(record.data);
                }
            }
        });

        this.downgrid = grid;
        return grid;
    },

    columsPreporGrid: function () {
        var colpg = Ext.create("Ext.grid.property.Grid", {
            renderTo: Ext.getBody(),
            border: false,
            source: {  }
        });

        this.MetadataColumnPG = colpg;
        return colpg;

    },

    showAddcolumnWin: function () {
        var tree = this.orgTree;
        var item = tree.getSelectionModel().getSelection();

        if (item == null || item.length != 1) {

            PSI.MsgBox.showInfo("请选择创建字段的码表!");
            return;
        }
        var islef = item[0].raw.leaf;

        if (islef == null) {
            PSI.MsgBox.showInfo("请选择创建字段的码表");
            return;
        }
        var parementid = item[0].raw.id;

        var form = Ext.create("PSI.BaseCode.Admin.Metadata.Column.MetadataColumnEditForm");

        form.show(parementid);

    }

});